package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsPermissionMapper;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsPermission;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.xml.crypto.Data;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class DtsRoleServiceImpl implements DtsRoleService {
    @Autowired
    DtsRoleMapper roleMapper;
    @Override
    public Set<String> findRolesByRoleIds(Integer[] roleIds) {
        LambdaQueryWrapper<DtsRole> wrapper = new LambdaQueryWrapper<>();
        //根据id  in查询
        wrapper.in(DtsRole::getId , roleIds);
        // 查询没有被逻辑删除
        wrapper.eq(DtsRole::getDeleted, false);
        List<DtsRole> roleList = roleMapper.selectList(wrapper);
        // stream 流进行转换数据
        Set<String> roles = roleList.stream().map((role) -> {
            return role.getName();
        }).collect(Collectors.toSet());
        return roles;
    }
    @Override
    public List<DtsRole> findAll() {
        LambdaQueryWrapper<DtsRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsRole::getDeleted, false);
        List<DtsRole> roleList = roleMapper.selectList(wrapper);
        return roleList;
    }
    @Override
    public void insert(DtsRole role) {
        role.setAddTime(new Date());
        role.setUpdateTime(new Date());
        role.setDeleted(false);
        role.setEnabled(true);
        roleMapper.insert(role);
    }
    @Override
    public Map<String, Object> queryPage(Integer page, Integer limit, String sort, String order, String rolename) {
        //分页条件
        IPage<DtsRole> iPage = new Page(page,limit);
        //其他查询条件
        QueryWrapper<DtsRole> wrapper = new  QueryWrapper<>();
        if (!StringUtils.isEmpty(rolename)){
            wrapper.like("name",rolename);
        }
        if (!StringUtils.isEmpty(order) && !StringUtils.isEmpty(sort)){
            if("desc".equals(order)){
                wrapper.orderByDesc(sort);
            }else {
                wrapper.orderByAsc(sort);
            }
        }
        roleMapper.selectPage(iPage,wrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("items",iPage.getRecords());
        return map;
    }

    @Override
    public boolean isSuperAdmin(Integer roleId) {
        DtsRole role = roleMapper.selectById(roleId);
        return role.getName().equals("超级管理员");
    }
    @Autowired
    DtsPermissionMapper permissionMapper;
    @Override
    public Set<String> queryStringPermissions(Integer roleId) {
        LambdaQueryWrapper<DtsPermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsPermission::getRoleId, roleId);
        wrapper.eq(DtsPermission::getDeleted, false);
        List<DtsPermission> permissionList = permissionMapper.selectList(wrapper);
        //数据转换
        Set<String> perms = permissionList.stream().map((permission) -> {
            return permission.getPermission();
        }).collect(Collectors.toSet());
        return perms;
    }
    @Override
    public void update(DtsRole role) {
        role.setUpdateTime(new Date());
        roleMapper.updateById(role);
    }

    @Override
    public void delete(DtsRole role) {
        // 逻辑删除
        role.setDeleted(true);
        roleMapper.updateById(role);
    }

}